using UnityEngine;
using QFramework;
using System;
using UnityEditor;
using QFramework.Example;
using Unity.VisualScripting.Antlr3.Runtime.Tree;
// 1.请在菜单 编辑器扩展/Namespace Settings 里设置命名空间
// 2.命名空间更改后，生成代码之后，需要把逻辑代码文件（非 Designer）的命名空间手动更改
namespace QFramework.Example
{
	public partial class Player : ViewController,IController
	{
        private static Player _instance;
        public static Player Instance => _instance;


        private bool isDie = false;
        private bool mFaceRight = true;

        /// <summary>
        /// 超级移动计时器
        /// </summary>
        private float SuperMove = 0;
        public Vector3 dic;

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;
        }

        private void Awake()
        {
            if (_instance==null)
            {
                _instance = this;
            }
        }


        void Start()
		{
            // Code Here

            HitHurtBox.OnTriggerEnter2DEvent(controller =>
            {



            }).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		void Update() 
		{
           

            if (DataManager.Instance.playerModel.PlayerSuperMove.Value==true)
            {
                PlayerRun();
            }
            else
            {
                PlayerMove();
            }

            GetMousePos();
        }



        void GetMousePos()
        {

            if (Time.timeScale>0)
            {
                var mouseScreenPosition = Input.mousePosition;

                var mouseWorldPosition = Camera.main.ScreenToWorldPoint(mouseScreenPosition);

                var shootDic = (mouseWorldPosition - transform.position).normalized;

                var radius = Mathf.Atan2(shootDic.y, shootDic.x);

                // radius * MathF.R

                var eulerAngle = radius * Mathf.Rad2Deg;


                Wapens.GetDic(shootDic);
                Wapens.transform.localRotation = Quaternion.Euler(0f, 0f, eulerAngle);

                if (shootDic.x > 0)
                {
                    Wapens.transform.localScale = new Vector3(1, 1, 1);
                }
                else
                {
                    Wapens.transform.localScale = new Vector3(1, -1, 1);
                }
            }

           

            

           

           
        }

        void PlayerRun()
        {
            SuperMove += Time.deltaTime;
            if (SuperMove >= 6f)
            {
                ///发布取消命令
                SuperMove = 0;
                this.SendCommand<SuperMoveCommands>();
            }
            else
            {
                if (!isDie)
                {
                    var x = Input.GetAxis("Horizontal");
                    var y = Input.GetAxis("Vertical");

                    this.transform.Translate(new Vector2(x, y).normalized * Time.deltaTime *
                        DataManager.Instance.playerModel.PlayerMoveSpeed.Value);
                    if (x == 0 && y == 0)
                    {
                        if (mFaceRight)
                        {
                            Body.Play("RightIdle");
                        }
                        else
                        {
                            Body.Play("LeftIdle");
                        }
                    }
                    else
                    {
                        if (x > 0)
                        {
                            mFaceRight = true;
                        }
                        else if (x < 0)
                        {
                            mFaceRight = false;
                        }


                        if (mFaceRight)
                        {
                            Body.Play("RightRun");
                        }
                        else
                        {
                            Body.Play("LeftRun");
                        }
                    }
                }
                else
                {
                    this.transform.Translate(Vector2.zero);
                }
            }
        }

		void PlayerMove()
		{
            

           

            if (!isDie)
            {
                var x = Input.GetAxis("Horizontal");
                var y = Input.GetAxis("Vertical");

                this.transform.Translate(new Vector2(x, y).normalized * Time.deltaTime *
                    DataManager.Instance.playerModel.PlayerMoveSpeed.Value);
                if (x==0&&y==0)
                {
                    if (mFaceRight)
                    {
                        Body.Play("RightIdle");
                    }
                    else
                    {
                        Body.Play("LeftIdle");
                    }
                }
                else
                {
                    if (x > 0)
                    {
                        mFaceRight = true;
                    }
                    else if (x < 0)
                    {
                        mFaceRight = false;
                    }


                    if (mFaceRight)
                    {
                        Body.Play("RightMove");
                    }
                    else
                    {
                        Body.Play("LeftMove");
                    }
                }


            }
            else
            {
                this.transform.Translate(Vector2.zero);
            }
        }
	}
}
